<template>
  <div style="padding:30px;">
    <div class="app-container">
      <div class="filter-container">
        <el-input v-model="querySn" placeholder="订单号" style="width: 200px; margin: 5px 0 5px 5px" class="filter-item" @keyup.enter.native="handleFilter"/>
        <el-input v-model="queryTradeOrderSn" placeholder="1688订单号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-select v-model="queryStats" placeholder="状态" clearable style="width: 120px" class="filter-item">
          <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item" />
        </el-select>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter" />
        <el-select v-model="queryType" placeholder="类型" clearable style="width: 150px; margin: 5px 0 5px 0" class="filter-item">
          <el-option v-for="item in typeOptions" :key="item" :label="item" :value="item"/>
        </el-select>
<!--        <el-button class="filter-item" style="margin: 5px 0 5px 0" type="primary" icon="el-icon-search" @click="handleFilter"/>-->
        <br>
      </div>
<!--      列表页包含ID/商品图片/商品名称/商品价格/进货地址/商品订单号/买家收货信息/物流查询/状态/查询/筛选-->
      <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-form  label-position="left" inline class="yl-table-expand">
              <el-form-item label="支付订单号">
                  <span>{{ scope.row.sn }}</span>
              </el-form-item>
              <el-form-item label="拼团成功会员ID">
                  <span>{{ scope.row.memeber_id }}</span>
              </el-form-item>
              <el-form-item label="收货人姓名">
                <span>{{ scope.row.address_info.mobile }}</span>
              </el-form-item>
              <el-form-item label="收货人电话">
                <span>{{ scope.row.address_info.mobile }}</span>
              </el-form-item>
              <el-form-item label="收货人地址">
                <span>{{ scope.row.address_info.province }}{{ scope.row.address_info.area }}{{ scope.row.address_info.street }}{{ scope.row.address_info.location }}</span>
              </el-form-item>
            </el-form>
            <el-form  label-position="left" inline class="yl-table-expand">
              <el-form-item label="支付订单号">
                <template>

                </template>
              </el-form-item>
              <el-form-item label="拼团失败会员">

              </el-form-item>

            </el-form>
          </template>
        </el-table-column>

        <el-table-column label="订单号" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.sn }}
          </template>
        </el-table-column>
        <el-table-column label="图片" width="120" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.product_info.img_url"  min-width="70" height="70" />
          </template>
        </el-table-column>
        <el-table-column label="1688订单号" width="80" align="center">
          <template slot-scope="scope">
            {{ scope.row.trade_order_sn }}
          </template>
        </el-table-column>
        <el-table-column label="产品编号" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.product_id }}
          </template>
        </el-table-column>
        <el-table-column label="产品名称" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.product_info.title }}
          </template>
        </el-table-column>
        <el-table-column label="消费金额" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.amount }}
          </template>
        </el-table-column>
        <el-table-column label="订单类型" width="110" align="center">
          <template slot-scope="{row}">
            <el-tag :type="row.type | typeCssFilter">
              {{ row.type | typeFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="80" align="center">
          <template slot-scope="{row}">
              <el-tag :type="row.stats | statsCssFilter">
                  {{ row.stats | statsFilter }}
              </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="评价状态" width="80" align="center">
          <template slot-scope="{row}">
            <el-tag :type="row.rate_stats | rateStatsCssFilter">
              {{ row.rate_stats | rateStatsFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" width="180" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.created_at }}</span>
          </template>
        </el-table-column>
        <el-table-column label="发起退款时间" width="180" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.refunded_at }}</span>
          </template>
        </el-table-column>
        <el-table-column label="发起退货时间" width="180" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.returned_at }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="currentPage" :limit.sync="pageSize" @pagination="fetchData({})"/>

    </div>
  </div>
</template>

<script>
import { getList } from '@/api/order'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  filters: {
    statsFilter(stats) {
      const statsMap = {
        0: '待支付',
        1: '待发货',
        2: '已发货',
        3: '确认收货'
      }
      return statsMap[stats]
    },
    statsCssFilter(stats) {
      const statsMap = {
        0: 'success',
        1: 'info',
        2: 'warn',
        3: 'danger',
      }
      return statsMap[stats]
    },
    refundStatsFilter(refund_stats) {
      const refundstatsMap = {
        0: '不退款不退货',
        1: '发起退款',
        2: '发起退货',
        3: '退款成功',
        4: '退货成功',
        5: '退款失败',
        6: '退货失败'
      }
      return refundstatsMap[refund_stats]
    },
    refundStatsCssFilter(refund_stats) {
      const refundstatsMap = {
        0: 'info',
        1: 'warn',
        2: 'warn',
        3: 'success',
        4: 'success',
        5: 'danger',
        6: 'danger'
      }
      return refundstatsMap[refund_stats]
    },
    rateStatsCssFilter(rate_stats) {
      const ratestatsMap = {
        0: '买家未评价',
        1: '买家已评价',
      }
      return ratestatsMap[rate_stats]
    },
    rateStatsCssFilter(rate_stats) {
      const ratestatsMap = {
        0: 'info',
        1: 'success',
      }
      return ratestatsMap[rate_stats]
    },
    typeFilter(type) {
      const typeMap = {
        1: '兑换',
        0: '购买'
      }
      return typeMap[type]
    },
    typeCssFilter(type) {
      const typeMap = {
        1: 'success',
        0: ''
      }
      return typeMap[type]
    }
  },
  data() {
    return {
      hidden: false,
      listLoading: true,
      // 翻页相关数据 当前页，每页显示条数，排序方式，排序字段，符合条件的搜索结果数，搜索结果
      currentPage: 1,
      pageSize: 10,
      sort: 'asc',
      order: 'created_at',
      total: 0,
      list: [],
      querySn: '',
      queryNickname: '',
      queryMobile: '',
      queryAccount: '',
      queryType: '',
      queryStats: '',
      date: '',
      sortBy: '',
      // 搜索条件相关数据
      typeOptions: ['金币转入', '金币消费', '金币转余额'],
      showReviewer: false,
      // 弹出层数据
      // 是否隐藏弹出层
      dialogFormVisible: false,
      // 临时数据
      temp: {
        id: undefined
      },
      // 上级分类数据
      parentArr: {},
      rules: {},
      // 下载的loading
      downloadLoading: false,
      downloadData: []
    }
  },
  mounted() {},
  created() {
    this.fetchData()
  },
  methods: {
    handleFilter() {
      this.currentPage = 1
      this.fetchData()
    },
    handleDownload() {
      this.downloadLoading = true
      this.downloadLoading = false
    },
    formatJson(filterVal) {
    },
    fetchData() {
      this.listLoading = true
      const param = {
        sort: this.sort,
        order: this.order,
        page: this.currentPage,
        page_size: this.pageSize
      }
      if (this.querySn !== '') {
        param.sn = this.querySn
      }
      if (this.queryNickname !== '') {
        param.nickname = this.queryNickname
      }
      if (this.queryMobile !== '') {
        param.mobile = this.queryMobile
      }
      if (this.queryType !== '') {
        this.typeOptions.map((item, idx) => {
          if (this.queryType === item) {
            param.type = idx
          }
        })
      }
      if (this.date !== '' && this.date !== null) {
        param.start_created_at = this.date[0].toLocaleDateString() + ' ' + this.date[0].toTimeString().split(' ')[0]
        param.end_created_at = this.date[1].toLocaleDateString() + ' ' + this.date[1].toTimeString().split(' ')[0]
      }
      console.log(param)
      getList(param).then(response => {
        const {data, meta} = response
        this.list = data
        this.listLoading = false
        this.currentPage = parseInt(meta.current_page)
        this.total = parseInt(meta.total)
      }).catch(() => {
        this.listLoading = false
        this.list = []
      })
    }
  }
}
</script>

<style scoped>
  .filter-container {
    background: #fff;
    padding: 32px 16px;
  }
</style>
